package com.tianma.web.service.master.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.tianma.common.core.domain.entity.SysUser;
import com.tianma.common.utils.StringUtils;
import com.tianma.common.utils.DateUtils;
import com.tianma.system.mapper.SysUserMapper;
import com.tianma.web.domain.master.file.WeekEvent;
import com.tianma.web.mapper.master.WeekEventMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import com.tianma.common.utils.poi.ExcelUtil;
import static com.tianma.common.utils.poi.ExcelUtil.getCellValue;
import com.tianma.common.utils.SecurityUtils;
import com.tianma.web.mapper.master.WeekEventDetailMapper;
import com.tianma.web.domain.master.file.WeekEventDetail;
import com.tianma.web.service.master.IWeekEventDetailService;


/**
 * 周事件明细Service业务层处理
 *
 * @author wwf
 * @date 2025-05-20
 */
@Service
public class WeekEventDetailServiceImpl implements IWeekEventDetailService
{
    public static Logger logger= LoggerFactory.getLogger(WeekEventDetailServiceImpl.class);

    @Autowired
    private WeekEventMapper weekEventMapper;
    @Autowired
    private WeekEventDetailMapper weekEventDetailMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询周事件明细
     *
     * @param id 周事件明细主键
     * @return 周事件明细
     */
    @Override
    public WeekEventDetail selectWeekEventDetailById(Integer id)
    {
        return weekEventDetailMapper.selectWeekEventDetailById(id);
    }

    /**
     * 查询周事件明细列表
     *
     * @param weekEventDetail 周事件明细
     * @return 周事件明细
     */
    @Override
    public List<WeekEventDetail> selectWeekEventDetailList(WeekEventDetail weekEventDetail)
    {
        return weekEventDetailMapper.selectWeekEventDetailList(weekEventDetail);
    }

    /**
     * 模糊查询周事件明细列表
     *
     * @param weekEventDetail 周事件明细
     * @return 周事件明细
     */
    @Override
    public List<WeekEventDetail> selectWeekEventDetailListByLike(WeekEventDetail weekEventDetail)
    {
        return weekEventDetailMapper.selectWeekEventDetailListByLike(weekEventDetail);
    }

    /**
     * 模糊查询周事件明细单例
     *
     * @param weekEventDetail 周事件明细
     * @return 周事件明细
     */
    @Override
    public WeekEventDetail selectWeekEventDetailByOne(WeekEventDetail weekEventDetail)
    {
        return weekEventDetailMapper.selectWeekEventDetailByOne(weekEventDetail);
    }

    /**
     * 新增周事件明细
     *
     * @param weekEventDetail 周事件明细
     * @return 结果
     */
    @Override
    public int insertWeekEventDetail(WeekEventDetail weekEventDetail)
    {
        weekEventDetail.setCreateTime(DateUtils.getNowDate());
        weekEventDetail.setCreateUser(SecurityUtils.getUsername());
        return weekEventDetailMapper.insertWeekEventDetail(weekEventDetail);
    }

    /**
     * 修改周事件明细
     *
     * @param weekEventDetail 周事件明细
     * @return 结果
     */
    @Override
    public int updateWeekEventDetail(WeekEventDetail weekEventDetail)
    {
        weekEventDetail.setUpdateUser(SecurityUtils.getUsername());
        return weekEventDetailMapper.updateWeekEventDetail(weekEventDetail);
    }

    /**
     * 批量删除周事件明细
     *
     * @param ids 需要删除的周事件明细主键
     * @return 结果
     */
    @Override
    public int deleteWeekEventDetailByIds(Integer[] ids)
    {
        return weekEventDetailMapper.deleteWeekEventDetailByIds(ids);
    }

    /**
     * 删除周事件明细信息
     *
     * @param id 周事件明细主键
     * @return 结果
     */
    @Override
    public int deleteWeekEventDetailById(Integer id)
    {
        return weekEventDetailMapper.deleteWeekEventDetailById(id);
    }

    /**
     * 删除全部周事件明细信息
     *
     * @return 结果
     */
    @Override
    public int removeAll()
    {
        return weekEventDetailMapper.removeAll();
    }


    /**
     * 上传周事件明细Excel
     *
     */
    @Override
    public void uploadWeekEventDetailExcel(Workbook workbook)
    {
        //TODO 自己实现业务功能
        Sheet sheet = workbook.getSheetAt(0);
        //获取Excel标题名对应导入字段
        Map<String, String> objectFieldNameMap = ExcelUtil.getObjectFieldNameMap(WeekEventDetail.class);
        //获取标题行索引
        Row titleRow = sheet.getRow(0);
        Map<String, Integer> titleIndexMap = ExcelUtil.getTitleIndexMap(titleRow);
        //设置对象值值
        int lastRowNum = sheet.getLastRowNum();
        for(int rowIndex=1;rowIndex<=lastRowNum;rowIndex++){
            Row row = sheet.getRow(rowIndex);
            String  str = getCellValue(row, 0).toString();
            if(StringUtils.isEmpty(str)){
                continue;
            }
            WeekEventDetail weekEventDetail=new WeekEventDetail();
            insertWeekEventDetail(weekEventDetail);
        }
    }

    @Override
    public int updateWeekEventDetailNew(WeekEventDetail weekEventDetail) {
        Integer eventId = weekEventDetail.getEventId();
        String nextNickName = weekEventDetail.getNextUserName();
        String fileName = weekEventDetail.getFileName();
        String fileId = weekEventDetail.getFileId();
        String disposeContext = weekEventDetail.getDisposeContext();
        String username = SecurityUtils.getNickname();

        //获取主表记录
        WeekEvent weekEvent = weekEventMapper.selectWeekEventById(eventId);
        if(weekEvent!=null){
            weekEvent.setEventStatus(2);//办结
            weekEvent.setProcessUser("");//下一处理人
            weekEvent.setUpdateUser(username);
            weekEvent.setUpdateTime(new Date());
            weekEventMapper.updateWeekEvent(weekEvent);
        }
        //插入办结记录
        WeekEventDetail condition=new WeekEventDetail();
        condition.setEventId(eventId);
        condition.setOperationType("办结");
        condition.setDataType(1);
        condition.setUserName(username);
        condition.setDisposeContext(disposeContext);
        condition.setFileName(fileName);
        condition.setFileId(fileId);
        insertWeekEventDetail(condition);

        return 1;
    }

    @Override
    public String processUserHandler(String processUserList) {
        String[] split = processUserList.split(",");
        String nickName = "";
        for (String userName : split) {
            //查找别名
            SysUser sysUser = sysUserMapper.selectUserByUserName(userName);
            nickName += sysUser.getNickName() + ",";
        }
        if (StringUtils.isNotEmpty(nickName)) {
            nickName = nickName.substring(0, nickName.length() - 1);
        }
        return nickName;
    }
}
